﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["reloadScript"] != null)
        {
            Response.Write(Session["reloadScript"]);
            Session["reloadScript"] = null;
        }
        Session["userName"] = User.Identity.Name.ToString();


        /*
        if (User.Identity.Name.ToString().Length != 0 && Session["action"] == "登入")
        {
            Session["time"] = DateTimeOffset.Now;
            SqlDataSource1.Insert();
        }
        Session["action"] = "";
        */
    }
    protected void Login_LoggedIn(object sender, EventArgs e)
    {
        /*
        Session["user"] = User.Identity.Name;
        Session.Timeout = 10;
        Session["action"] = "登入";
        */
        Session["reloadScript"] = "<script>parent.mainframe.location.href='Management/indexInManagementAfter.aspx';</script>";
    }
    protected void LoginStatus_LoggedOut(object sender, EventArgs e)
    {
        Session["reloadScript"] = "<script>parent.mainframe.location.href='Management/indexInManagement.aspx';</script>";
        Session["Tree1"] = null;
    }
    private void InitTree()
    {
        TreeView Tree1 = new TreeView();
        TreeNode TemNode = new TreeNode();

        TemNode.Text = "首頁";
        TemNode.Value = "0";
        TemNode.NavigateUrl = "~/Management/indexInManagement.aspx";
        TemNode.Target = "mainframe";

        Tree1.Nodes.Add(TemNode);

        Session["Tree1"] = Tree1;
    }
    private void BuildTree()
    {
        TreeView Tree1 = new TreeView();

        if (Session["Tree1"] == null)
        {
            InitTree();
        }

        Tree1 = (TreeView)Session["Tree1"];
        TreeNode RootNode = new TreeNode();
        RootNode = Tree1.Nodes[0];

        String rc;
        rc = AddNodes(RootNode, 0);
        //Label1.Text = rc;
        Session["Tree1"] = Tree1;
    }
    protected string AddNodes(TreeNode tNode, decimal PID)
    {
        try
        {
            if (Session["dt"] == null)
            {
                GetDataTable();
            }
            DataTable dt;
            dt = (DataTable)Session["dt"];
            DataRow[] rows;
            string filterExpr;
            filterExpr = "ParentId =" + PID;
            rows = dt.Select(filterExpr);
            if (rows.GetUpperBound(0) >= 0)
            {
                DataRow row;
                decimal tmpNodeId;
                string tmpsText;
                string tmpsValue;
                string tmpsUrl;
                string tmpsTarget;
                TreeNode NewNode;
                string rc;
                for (int i = 0; i < rows.Length; i++)
                {
                    row = rows[i];
                    tmpNodeId = (decimal)row[0];
                    tmpsText = (string)row[2];
                    tmpsValue = (string)row[3];
                    tmpsUrl = (string)row[4];
                    tmpsTarget = (string)row[5];
                    NewNode = new TreeNode();
                    NewNode.Text = tmpsText;
                    NewNode.Value = tmpsText;
                    NewNode.NavigateUrl = tmpsUrl;
                    NewNode.Target = tmpsTarget;
                    tNode.ChildNodes.Add(NewNode);
                    rc = AddNodes(NewNode, tmpNodeId);

                }
            }
            return "Success";
        }
        catch (Exception ex)
        {
            this.Label1.Text = ex.Message.ToString();
            return "False";
        }
    }
    protected void GetDataTable()
    {
        string ConnStr;
        SqlConnection Conn;
        SqlDataAdapter Da;
        DataSet Ds;
        DataTable dt;
        string sqlTxt;
        try
        {
            //取得web.config的連線字串
            ConnStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AccountAuthConnectionString"].ToString();
            Conn = new SqlConnection(ConnStr);
            Conn.Open();
            sqlTxt = "Select * from AuthTreeNode ";
            Da = new SqlDataAdapter(sqlTxt, Conn);
            Ds = new DataSet();
            Da.Fill(Ds);
            dt = new DataTable();
            dt = Ds.Tables[0];
            Session["dt"] = dt;
            Conn.Close();
        }
        catch (Exception ex)
        {
            this.Label1.Text = ex.Message.ToString();
        }

    }
}